<template>
  <dialog-box
    :isDialog="false"
    ref="myDialog"
    :showFooter="true"
    width="600px"
    cacelName="关闭"
    @close="close"
    :isFooterSlot="true"
    :isShowSave="false"
    :title="state.title"
  >
    <template v-slot:dialogMain>
      <div>
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          label-width="180px"
          class="demo-ruleForm"
          size="default"
          status-icon
        >
          <el-form-item label="下载任务名称" prop="name">
            <div class="text-box">{{ queryFrom.name || "" }}</div>
          </el-form-item>
          <el-form-item label="下载任务说明" prop="name">
            <div class="text-box">{{ queryFrom.dscr || "" }}</div>
          </el-form-item>
          <el-form-item label="取数类型" prop="name">
            <div class="text-box">{{ getListLabel(queryFrom.type) }}</div>
          </el-form-item>
          <el-form-item label="API" prop="name">
            <div class="text-box">{{ queryFrom.api || "" }}</div>
          </el-form-item>
        </el-form>
      </div>
    </template>
  </dialog-box>
</template>

<script setup lang="ts">
import dialogBox from "@/components/dialog-box/index.vue";
import { reactive, toRefs, ref } from "vue";
import type { FormInstance } from "element-plus";
import { PAth } from "@/api/SelfFetch/query";
const queryFrom = ref(PAth());
const myDialog = ref(null);

const ruleFormRef = ref<FormInstance>();

const ruleForm = reactive({
  name: "",
});

let state = reactive({
  title: "",
  fetchType: [
    {
      label: "立即取数",
      key: "0",
    },
    {
      label: "预约取数",
      key: "1",
    },
  ],
});
// 定义完成后，用defineExpose()暴露给父组件
const open = (title: string, row: any) => {
  state.title = title;
  for (const key in row) {
    if (Object.prototype.hasOwnProperty.call(queryFrom.value, key)) {
      queryFrom.value[key] = row[key];
    }
  }
  myDialog.value.handleOpen();
};
const close = () => {
  resetForm();
  myDialog.value.handleClose();
};
defineExpose({
  open,
  close,
});

const getListLabel = (val: any) => {
  let value = ''
  if(val){
    let arr = state.fetchType.filter(it=>it.key==val)
    value = arr && arr.length > 0 ? arr[0].label : '';
  }
  return value;
};
const resetForm = () => {
  if (!ruleFormRef) return;
  ruleFormRef.value?.resetFields();
};
</script>

<style scoped>
.text-box{
  word-break: break-all;
}
</style>
